package com.ibeetl.admin.core.web;


import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.rbac.tree.DictItem;
import com.ibeetl.admin.core.service.CoreDictService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.web.dto.DictNodeView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@SuppressWarnings("unchecked")
public class CoreDictController {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/core/dict";

    @Autowired
    CorePlatformService platformService;

    @Autowired
    CoreDictService dictService;

 

    /**
     * 查看字典类型对应的列表
     * @param type
     * @return
     */
    @RequestMapping(MODEL + "/view.json")
    @ResponseBody
    public JsonResult<List<CoreDict>> view(String type) {
        List<CoreDict> list = dictService.findAllByType(type);
        return  JsonResult.success(list);
    }
    
    /**
     * 查看字典值的子字典
     * @param value
     * @return
     */
  
    @RequestMapping(MODEL + "/viewChildren.json")
    @ResponseBody
    public JsonResult<List<CoreDict>> viewChild(String type,String value) {
        CoreDict dict = dictService.findCoreDict(type,value);
        if (dict != null) {
            List<CoreDict> list = dictService.findChildByParent(dict.getId());
            return  JsonResult.success(list);
        }
        return  JsonResult.success();
    }

    /**
     * 获取系统的栏目树 用于表格树的展示
     *
     * @return
     */
    @RequestMapping(MODEL + "/tableTree.json")
    @ResponseBody
    public JsonResult<List<DictNodeView>> getDictTableTree(String type) {
        DictItem dictItem = platformService.buildDictTableTree(type);
        List<DictNodeView> view = this.buildTree(dictItem);
        return JsonResult.success(view);
    }
    private List<DictNodeView> buildTree(DictItem node) {
        List<DictItem> list = node.getChildren();
        if (list.size() == 0) {
            return Collections.EMPTY_LIST;
        }

        List<DictNodeView> views = new ArrayList<DictNodeView>(list.size());

        for (DictItem item : list) {
            DictNodeView view = new DictNodeView();
            if (item.getParent() != null) {
                view.setParent(item.getData().getParent());
            }
            view.setType(item.getData().getType());
            view.setName(item.getData().getName());
            view.setValue(item.getData().getValue());
            view.setId(item.getData().getId());
            view.setTypeName(item.getData().getTypeName());
            view.setSort(item.getData().getSort());
            List<DictNodeView> children = this.buildTree(item);
            view.setChildren(children);
            views.add(view);
        }
        return views;
    }

}
